﻿/// <remarks>
///系统名称            :   ITALKI 翻译系统
///模块名称            :   Translate
///文件代码            :   
///功能概述            :   
///创建人              :   Snail
///创建日期            :   2008.08.15
///版本号              :   1.0.0.0
///----------< 修改履历 >---------------------------------------------------------------
///修改日期          修改人              修改内容                           版本号
///------------------------------------------------------------------------------------
/// (C) 2007-2008 上海微语信息技术有限公司  版权所有。保留所有权利。
///------------------------------------------------------------------------------------
///</remarks>

using System;
using System.Text;
using System.Web;
using System.Xml;
using BLF.WWW_WWW.translate;
using System.Collections;
using System.Data;
using Lx.Web.Common.ControlsUI;
using Lx.Web.Common.UI;
using Model.WWW_WWW.translate;
using System.IO;
using System.Data.SqlClient;


namespace Web.Control.WWW.translate
{
    /// <summary>
    /// 页面类。
    /// </summary>
    public partial class export : BaseControl
    {
        protected ArrayList LangSets = new ArrayList();
        protected TranslateMember CurrentMember;

        protected System.Web.UI.WebControls.PlaceHolder plh1;
        protected System.Web.UI.WebControls.PlaceHolder plh2;
        protected System.Web.UI.WebControls.Literal litLink;

        protected override void OnLoad(EventArgs e)
        {
            //判断是否登录
            if (HttpContext.Current.User.Identity.Name == "")
            {
                Response.Redirect(WWWHost + "/User/login.htm?ReturnUrl=" + Request.Url.AbsoluteUri.Split('?')[0]);
            }

            //判断其是否为翻译成员并且是否为管理员
            CurrentMember = Translate.GetTranslateMember(HttpContext.Current.User.Identity.Name);
            if (CurrentMember == null || !CurrentMember.IsAdmin)
                Response.Redirect(WWWHost + "/translate/default.htm");


            //读取语言设置XML
            XmlDocument xDoc = new XmlDocument();
            try
            {
                xDoc.Load(string.Format(Server.MapPath("LangSet/LangSet_{0}.xml"), Globals.Language));
            }
            catch
            {
                xDoc.Load(string.Format(Server.MapPath("LangSet/LangSet_en-us.xml"), Globals.Language));
            }
            XmlNodeList xnl = xDoc.SelectNodes("//LangSet/Lang");
            if (xnl != null)
                foreach (XmlNode xn in xnl)
                {
                    LangSets.Add(new string[] { xn.ChildNodes[0].InnerText, xn.ChildNodes[1].InnerText });
                }

            if (plh1 != null) plh1.Visible = true;
            if (plh2 != null) plh2.Visible = false;
            //导出数据
            string chklangs;
            if (!string.IsNullOrEmpty(chklangs = Request.Form["chklangs"]))
            {
                string[] strlangs = chklangs.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
                //Hashtable items = Translate.ExportTranslateItems(strlangs);
                //if (items != null && items.Count > 0)
                //{
                //    StringBuilder sb = new StringBuilder();
                //    foreach (DictionaryEntry de in items)
                //    {
                //        sb.Append(de.Key + "=" + de.Value + "\r\n");
                //    }

                //    byte[] data = Encoding.UTF8.GetBytes(sb.ToString());
                //    Context.Response.ContentType = "application/octet-stream";
                //    Context.Response.AppendHeader("Content-Disposition", "attachment;filename=translate_export.txt");
                //    Context.Response.AddHeader("Content-Length", data.Length.ToString());

                //    Response.BinaryWrite(data);
                //    Response.End();
                //    Response.Close();
                //}
                if (plh1 != null) plh1.Visible = false;
                if (plh2 != null) plh2.Visible = true;
                DataTable items = Translate.ExportTranslateItems(strlangs);
                if (items != null && items.Rows.Count > 0)
                {
                    StringBuilder sb = null;
                    string tmpText = null;
                    string tmpPath = null;
                    DataView dv = new DataView(items);
                    for (int i = 0; i < strlangs.Length; i++)
                    {
                       DataRow[] drs = items.Select("targetlang='" + strlangs[i]+"'");
                       sb = new StringBuilder();
                       for (int k = 0; k < drs.Length; k++)
                       {
                           sb.Append(drs[k][0].ToString() + "=" + drs[k][1].ToString() + "\r\n");
                       }
                       tmpText = sb.ToString();
                       if (tmpText.Trim() != "")
                       {
                           tmpPath = "/translate/txt/all." + strlangs[i] + ".txt";
                           File.WriteAllText(Server.MapPath(tmpPath), tmpText, Encoding.UTF8);
                           if (litLink != null)
                           {
                               litLink.Text += "<a href='/translate/txt/all." + strlangs[i] + ".txt' target='blank'>" + GetLangText(strlangs[i]) + "</a><br/>";
                           }
                       }
                        //
                    }
                }
                return;
            }
            base.OnLoad(e);
        }

        private string GetLangText(string lang)
        {
            if (LangSets == null || LangSets.Count == 0) return "";
            for (int i = 0; i < LangSets.Count; i++)
            {
                if (((string[])LangSets[i])[0].ToLower() == lang.ToLower())
                {
                    return ((string[])LangSets[i])[1];
                }
            }
            return "";
        }
    }
}